package com.fyp.client.ptma.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.simpleframework.xml.core.Persister;

import com.fyp.client.ptma.model.ApplicationVersion;
import com.fyp.client.ptma.model.Installation;
import com.fyp.client.ptma.xml.ResponseXml;

public final class Helper
{
	public static Installation notification;
	public static ApplicationVersion appVersion;

	public static String encrypt(String strToEncrypt)
	{

		return strToEncrypt;
	}

	public static String decrypt(String strToDecrypt)
	{

		if (strToDecrypt == null || strToDecrypt.length() == 0)
		{
			return null;
		}
		StringBuilder decryptedStr = new StringBuilder();
		/*
		 * int length = strToDecrypt.size() - 1; int i = 0; for
		 * (Iterator<String> iterator = strToDecrypt.iterator(); iterator
		 * .hasNext(); i++) { if (i == length) { break; } String str =
		 * iterator.next(); decryptedStr.append((char) Integer.parseInt(str)); }
		 */

		return decryptedStr.toString();
	}

	public static final ResponseXml getResponseObject(HttpResponse response)
			throws Exception
	{
		InputStream is = response.getEntity().getContent();
		InputStreamReader ir = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(ir);
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null)
		{
			sb.append(line);
		}
		br.close();
		ir.close();
		is.close();
		Persister persister = new Persister();
		ResponseXml wsResponse = persister.read(ResponseXml.class,
				sb.toString());
		return wsResponse;
	}

}
